
/*
     Initialize and render the MenuBar when the page's DOM is ready 
     to be scripted.
 */

Y.util.Event.onDOMReady(function() {
	
	 var onClicAyuda = function(p_sType, p_aArgs, p_oValue) {
		 var form = getMainRedirectForm();
         var actionUrl = Y.redirects["welcome"];
         form.action = actionUrl;
         form.submit();
		  };  
  
  

    var onTMenuItemClick = function(p_sType, p_aArgs, p_oValue) {
        try {
            var form = getMainRedirectForm();
            var actionUrl = Y.redirects[p_oValue];
            if (actionUrl == undefined || actionUrl == null) {
                alert("Action URL no definida para  " + p_oValue + " en onTMenuItemClick");
            } else {
                form.action = actionUrl;
                form.submit();
            }
        } catch (e) {
            alert("onTMenuItemClick " + e + " " + e.description);
        }
    };

   

    /*
     * Define an array of object literals, each containing the data necessary to
     * create the items for a MenuBar.
     */

    var aItemData = null;

    try {
        var callback = new Z.JsonCall();
        callback.exito = function(messages) {
			
            var rows = messages.ResultSet;
			 
            if (Y.lang.isArray(rows)) {
            	asignarFnMenu(rows);
            }
            
            
            /*var fr = Y.util.Dom.getXY("ayudaredirect");
            if (fr){
            	
            	
                var ayuda = {text:"<em id=\"ayuda\">Ayuda</em>",onclick:{fn:onClicAyuda, obj:"ayuda"},disabled:false};
                rows.unshift(ayuda);
            } */     

            
            aItemData = rows;
            
            var oMenuBar = new Y.widget.MenuBar("x", {
                lazyload : true,
                itemdata : aItemData
            });
            /*
             * Since this MenuBar instance is built completely from script, call
             * the "render" method passing in a node reference for the DOM
             * element that its should be appended to.
             */
            //oMenuBar.render(document.body);
			oMenuBar.render("menuprincipal");
			
            // Add a "show" event listener for each submenu.
            function onSubmenuShow() {
                var oIFrame, oElement, nOffsetWidth;

                // Keep the left-most submenu against the left edge of the
                // browser
                // viewport

                if (this.id == "yahoo") {

                    Y.util.Dom.setX(this.element, 0);
                    oIFrame = this.iframe;
                    if (oIFrame) {
                        Y.util.Dom.setX(oIFrame, 0);
                    }
                    this.cfg.setProperty("x", 0, true);
                }

                /*
                 * Need to set the width for submenus of submenus in IE to
                 * prevent the mouseout event from firing prematurely when the
                 * user mouses off of a MenuItem's text node.
                 */

                if ((this.id == "filemenu" || this.id == "editmenu") && Y.env.ua.ie) {
                    oElement = this.element;
                    nOffsetWidth = oElement.offsetWidth;

                    /*
                     * Measuring the difference of the offsetWidth before and
                     * after setting the "width" style attribute allows us to
                     * compute the about of padding and borders applied to the
                     * element, which in turn allows us to set the "width"
                     * property correctly.
                     */

                    oElement.style.width = nOffsetWidth + "px";
                    oElement.style.width = (nOffsetWidth - (oElement.offsetWidth - nOffsetWidth)) + "px";
                }
            }
            // Subscribe to the "show" event for each submenu

            oMenuBar.subscribe("show", onSubmenuShow);
            
        };
        var request = Y.util.Connect.asyncRequest('POST', Y.redirects['menuasync'], callback);
    } catch (e) {
    	 alert("menu:" + e + " " + e.description)
    }
    
    function asignarFnMenu(menuArr){
        for(var j = 0; j < menuArr.length; j++) {
            var node = menuArr[j];
            if(node.submenu){
                asignarFnMenu(node.submenu.itemdata);
            }else{
                node.onclick.fn = onTMenuItemClick;
            }
        }
    }

});

Y.util.Event.onDOMReady(function() {
  try {
    
    Y.AyudaPnl = new Y.widget.Panel("ayudadlg", {
      width : "95%",
      fixedcenter : true,
      close : true,
      draggable : true,
      zindex : 5,
      modal : false,
      visible : false
    });
    var kl = new Y.util.KeyListener(document, {
      keys : 27
    }, {
      fn : Y.AyudaPnl.hide,
      scope : Y.AyudaPnl,
      correctScope : true
    });
    kl.enable();
    Y.AyudaPnl.render(document.body);
  } catch (e) {
    alert("AyudaPnl: " + e.description);
  }
});



